package com.tarena.lbs.coupon.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.coupon.dao.mapper.CouponMapper;
import com.tarena.lbs.coupon.dao.mapper.UserCouponsMapper;
import com.tarena.lbs.coupon.dao.repository.CouponRepository;
import com.tarena.lbs.coupon.pojo.param.CouponParam;
import com.tarena.lbs.coupon.pojo.po.CouponPO;
import com.tarena.lbs.coupon.pojo.po.UserCouponsPO;
import com.tarena.lbs.coupon.pojo.vo.CouponVO;
import com.tarena.lbs.coupon.pojo.vo.UserCouponsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CouponRepositoryImpl implements CouponRepository {

    @Autowired
    private CouponMapper couponMapper;
    @Autowired
    private UserCouponsMapper userCouponsMapper;

    @Override
    public Integer save(CouponPO couponParam) {

        return couponMapper.insert(couponParam);
    }


    @Override
    public PageInfo<CouponPO> pagesList(CouponParam couponParam) {
        QueryWrapper<CouponPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("isdel",0);
        if(couponParam.getCouponType()!=null &&!couponParam.getCouponType().equals(0)){
            queryWrapper.eq("coupon_type",couponParam.getCouponType());
        }
        if(couponParam.getEnableStatus() !=null && !couponParam.getEnableStatus().equals(0)){
            queryWrapper.eq("enable_status",couponParam.getEnableStatus());
        }
        if(couponParam.getCouponName()!=null && !couponParam.getCouponName().equals("")){
            queryWrapper.eq("coupon_name",couponParam.getCouponName());
        }
        PageHelper.startPage(1, 10);
        List<CouponPO> pos = couponMapper.selectList(queryWrapper);
        return new PageInfo<>(pos);
    }


    @Override
    public void UserCouponsSave(UserCouponsPO userCouponsPO) {
        userCouponsMapper.insert(userCouponsPO);
    }


    /**
     * 用户查询优惠券列表
     * @return
     */
    @Override
    public PageInfo<UserCouponsVO> pageUserCouponsList() {

        PageHelper.startPage(1, 10);
        List<UserCouponsVO> pos=userCouponsMapper.selectUserCouponsVOList();
        return new PageInfo<>(pos);
    }


    @Override
    public void updateCouponStatus(CouponParam couponParam) throws BusinessException {
        couponMapper.updateCouponStatus(couponParam);
    }

    @Override
    public void updateCoupon(CouponParam couponParam) throws BusinessException {
        couponMapper.updateCoupon(couponParam);
    }

    @Override
    public void deleteCoupon(CouponParam couponParam) {
        couponMapper.deleteCoupon(couponParam);

    }

    @Override
    public CouponVO detail(int CouponParam) {
        return couponMapper.detail(CouponParam);
    }
}
